﻿using AdminApi.Extension.Permission;
using AdminApi.Extension.Permission.Attributes;
using AdminApi.Extension.Permission.Model;
using Application.Admin;
using Domain.Types;
using DTO.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using Infrastructure.Error;

namespace AdminApi.Controllers
{

    //权限管理 = 1, 广告管理 = 2, 用户管理 = 3, 任务管理 = 4, 提现管理 = 5, 财务管理 = 6,
    [EnableCors("AllowSameDomain")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        public AdminApplication adminApplication { get; set; }
        public ILogger<AdminController> _logger { get; set; }

        #region 登录 

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="adminLoginInput"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Admin/Login")]
        [AllowAnonymous]
        public ActionResult Login(AdminLoginInput adminLoginInput)
        {
            try
            {
                var reps = adminApplication.Login(adminLoginInput);
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

        #endregion

        #region 管理员

        [HttpGet]
        [Route("api/Admin/GetAdminList")]
        [PerssionMenu("管理员管理", "权限管理", Index = "/adminmanager")]
        public ActionResult GetAdminList([FromQuery]GetAdminListReq req)
        {
            var data = adminApplication.GetAdminList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpPost]
        [Route("api/Admin/AddAdmin")]
        [PerssionMenu("添加", "权限管理-管理员管理", MenuType.添加)]
        public ActionResult AddAdmin(AdminInput input)
        {
            try
            {
                bool b = adminApplication.AddAdmin(input);
                if (b)
                    return this.AdminSuccess(true);
                else
                    return this.Error(false);
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }

        [HttpPost]
        [Route("api/Admin/EditAdmin")]
        [PerssionMenu("修改", "权限管理-管理员管理", MenuType.修改)]
        public ActionResult EditAdmin(AdminInput input)
        {
            bool b = adminApplication.EditAdmin(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/DelAdmin")]
        [PerssionMenu("删除", "权限管理-管理员管理", MenuType.删除)]
        public ActionResult DelAdmin(int id)
        {
            bool b = adminApplication.DelAdmin(id);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }


        [HttpGet]
        [Route("api/Admin/GetRoleSelect")]
        [PerssionMenu("角色下拉框", "权限管理-管理员管理", MenuType.查询)]
        public ActionResult GetRoleSelect([FromQuery]GetRoleListReq req)
        {
            req.IsAll = 1;
            var data = adminApplication.GetRoleList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }


        #endregion

        #region 角色

        [HttpGet]
        [Route("api/Admin/GetRoleList")]
        [PerssionMenu("角色管理", "权限管理", Index = "/rolemanager")]
        public ActionResult GetRoleList([FromQuery]GetRoleListReq req)
        {
            var data = adminApplication.GetRoleList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }


        [HttpGet]
        [Route("api/Admin/GetRolePerssion")]
        [PerssionMenu("权限分配", "权限管理-角色管理", MenuType.查询)]
        public ActionResult<TreeNodeDto> GetRolePerssion(int roleID)
        {
            var data = adminApplication.GetRolePerssion(roleID);
            return this.Success(PressionManager.GetRolePerssion(data), "成功");
        }

        [HttpPost]
        [Route("api/Admin/AddRole")]
        [PerssionMenu("添加", "权限管理-角色管理", MenuType.添加)]
        public ActionResult AddRole(RoleInput input)
        {
            try
            {
                bool b = adminApplication.AddRole(input);
                if (b)
                    return this.AdminSuccess(true);
                else
                    return this.Error(false);
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }

        [HttpPost]
        [Route("api/Admin/EditRole")]
        [PerssionMenu("修改", "权限管理-角色管理", MenuType.修改)]
        public ActionResult EditRole(RoleInput input)
        {
            bool b = adminApplication.EditRole(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/DelRole")]
        [PerssionMenu("删除", "权限管理-角色管理", MenuType.删除)]
        public ActionResult DelRole(int id)
        {
            bool b = adminApplication.DelRole(id);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/GetMenus")]
        [PerssionMenu("菜单列表", "权限管理-角色管理", MenuType.查询)]
        public ActionResult<TreeNodeDto> GetMenus()
        {
            Dictionary<string, object> payLoad = (Dictionary<string, object>)this.HttpContext.Items["payLoad"];
            var data = adminApplication.GetMenus(payLoad);
            var treeNode = PressionManager.GetAdminMenus(data);
            return this.AdminSuccess(treeNode, "成功");
        }

        #endregion

        #region 广告列表

        [HttpGet]
        [Route("api/Admin/GetCategorySelect")]
        [AllowAnonymous]
        public ActionResult GetCategorySelect()
        {
            return this.AdminSuccess(adminApplication.GetCategorySelect(), "加载成功");
        }


        [HttpGet]
        [Route("api/Admin/GetBannerList")]
        [PerssionMenu("广告列表", "广告管理", Index = "/bannermanager")]
        public ActionResult GetBannerList([FromQuery]GetBannerListReq req)
        {
            var data = adminApplication.GetBannerList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpPost]
        [Route("api/Admin/AddBanner")]
        [PerssionMenu("添加", "广告管理-广告列表", MenuType.添加)]
        public ActionResult AddBanner(BannerInput input)
        {
            bool b = adminApplication.AddBanner(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpPost]
        [Route("api/Admin/EditBanner")]
        [PerssionMenu("修改", "广告管理-广告列表", MenuType.修改)]
        public ActionResult EditBanner(BannerInput input)
        {
            bool b = adminApplication.EditBanner(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/DelBanner")]
        [PerssionMenu("删除", "广告管理-广告列表", MenuType.删除)]
        public ActionResult DelBanner(int id)
        {
            bool b = adminApplication.DelBanner(id);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }


        #endregion

        #region 用户管理

        [HttpGet]
        [Route("api/Admin/GetUserInfoList")]
        [PerssionMenu("用户列表", "用户管理", Index = "/usermanager")]
        public ActionResult GetUserInfoList([FromQuery]GetUserInfoListReq req)
        {
            var data = adminApplication.GetUserInfoList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpGet]
        [Route("api/Admin/GetUserWalletList")]
        [PerssionMenu("用户钱包", "用户管理", Index = "/userwallet")]
        public ActionResult GetUserWalletList([FromQuery]GetUserWalletListReq req)
        {
            var data = adminApplication.GetUserWalletList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpGet]
        [Route("api/Admin/GetInComeWalletLogByWalletId")]
        [PerssionMenu("收入日志", "用户管理-用户钱包", MenuType.查询)]
        public ActionResult GetInComeWalletLogByWalletId([FromQuery]GetWalletLogByWalletIdReq req)
        {
            var data = adminApplication.GetInComeWalletLogByWalletId(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpGet]
        [Route("api/Admin/GetChargeWalletLogByWalletId")]
        [PerssionMenu("充值记录", "用户管理-用户钱包", MenuType.查询)]
        public ActionResult GetChargeWalletLogByWalletId([FromQuery]GetWalletLogByWalletIdReq req)
        {
            var data = adminApplication.GetChargeWalletLogByWalletId(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpGet]
        [Route("api/Admin/GetBlackList")]
        [PerssionMenu("黑名单", "用户管理", Index = "/userblack")]
        public ActionResult GetBlackList([FromQuery]GetBlackListReq req)
        {
            var data = adminApplication.GetBlackList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }
        #endregion

        #region 财务管理

        [HttpGet]
        [Route("api/Admin/GetFinancialList")]
        [PerssionMenu("财务信息", "财务管理", Index = "/financialmanager")]
        public ActionResult GetFinancialList([FromQuery]GetFinancialListReq req)
        {
            var data = adminApplication.GetFinancialList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpGet]
        [Route("api/Admin/GetWithDrawList")]
        [PerssionMenu("提现管理", "财务管理", Index = "/withdrawmanager")]
        public ActionResult GetWithDrawList([FromQuery]GetWithDrawListReq req)
        {
            var data = adminApplication.GetWithDrawList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpPost]
        [Route("api/Admin/ExamineOkWithDraw")]
        [PerssionMenu("通过", "财务管理-提现管理", MenuType.修改)]
        public ActionResult ExamineOkWithDraw(int id)
        {
            var data = adminApplication.ExamineOkWithDraw(id);
            return this.AdminSuccess(data, "成功");
        }

        [HttpPost]
        [Route("api/Admin/ExamineErrorWithDraw")]
        [PerssionMenu("驳回", "财务管理-提现管理", MenuType.修改)]
        public ActionResult ExamineErrorWithDraw(int id)
        {
            var data = adminApplication.ExamineErrorWithDraw(id);
            return this.AdminSuccess(data, "成功");
        }


        #endregion

        #region 短信记录管理
        [HttpGet]
        [Route("api/Admin/GetSmsCodeList")]
        [PerssionMenu("短信列表", "短信记录管理", Index = "/smsmanager")]
        public ActionResult GetSmsCodeList([FromQuery]GetSmsCodeListReq req)
        {
            var data = adminApplication.GetSmsCodeList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }
        #endregion

        #region 系统消息管理

        [HttpGet]
        [Route("api/Admin/GetMessageList")]
        [PerssionMenu("系统消息", "系统消息管理", Index = "/messagemanager")]
        public ActionResult GetMessageList([FromQuery]GetMessageListReq req)
        {
            var data = adminApplication.GetMessageList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }
        #endregion

        #region 系统配置管理

        [HttpGet]
        [Route("api/Admin/GetDicList")]
        [PerssionMenu("字典列表", "系统配置管理", Index = "/systemmanager")]
        public ActionResult GetDicList([FromQuery]GetDicListReq req)
        {
            var data = adminApplication.GetDicList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpPost]
        [Route("api/Admin/AddDic")]
        [PerssionMenu("添加", "系统配置管理-字典列表", MenuType.添加)]
        public ActionResult AddDic(DicInput input)
        {
            bool b = adminApplication.AddDic(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpPost]
        [Route("api/Admin/EditDic")]
        [PerssionMenu("修改", "系统配置管理-字典列表", MenuType.修改)]
        public ActionResult EditDic(DicInput input)
        {
            bool b = adminApplication.EditDic(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/DelDic")]
        [PerssionMenu("删除", "系统配置管理-字典列表", MenuType.删除)]
        public ActionResult DelDic(int id)
        {
            bool b = adminApplication.DelDic(id);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }
        #endregion

        #region 客服系统管理

        [HttpGet]
        [Route("api/Admin/GetKFQAAggregateList")]
        [PerssionMenu("客服问答列表", "客服系统管理", Index = "/kfqammanager")]
        public ActionResult GetKFQAAggregateList([FromQuery]GetKFQAAggregateListReq req)
        {
            var data = adminApplication.GetKFQAAggregateList(req, out int count);
            return this.AdminSuccess(data, "加载成功", count);
        }

        [HttpPost]
        [Route("api/Admin/AddKFQA")]
        [PerssionMenu("添加", "客服系统管理-客服问答列表", MenuType.添加)]
        public ActionResult AddKFQA(KFQAInput input)
        {
            bool b = adminApplication.AddKFQA(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpPost]
        [Route("api/Admin/EditKFQA")]
        [PerssionMenu("修改", "客服系统管理-客服问答列表", MenuType.修改)]
        public ActionResult EditKFQA(KFQAInput input)
        {
            bool b = adminApplication.EditKFQA(input);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }

        [HttpGet]
        [Route("api/Admin/DelKFQA")]
        [PerssionMenu("删除", "客服系统管理-客服问答列表", MenuType.删除)]
        public ActionResult DelKFQA(int id)
        {
            bool b = adminApplication.DelKFQA(id);
            if (b)
                return this.AdminSuccess(true);
            else
                return this.Error(false);
        }



        #endregion
    }
}
